package com.swang.pms.web.controller.common;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.swang.pms.common.util.EnumToDataUtil;
import com.swang.pms.common.util.LoggerUtil;
import com.swang.pms.model.annotation.PltLog;
import com.swang.pms.model.common.Pager;
import com.swang.pms.model.dto.common.SysUrlWhiteListDto;
import com.swang.pms.model.entity.CurrentKey;
import com.swang.pms.model.enums.Status;
import com.swang.pms.model.enums.SysType;
import com.swang.pms.model.vo.ResultMessage;
import com.swang.pms.service.common.SysUrlWhiteListService;
import com.swang.pms.web.controller.BaseController;

/**
 * @ClassName: SysWhiteController
 * @Description: 系统白名单服务控制层
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
@Controller
@RequestMapping("/whitelist")
public class SysWhiteController extends BaseController {
    private static final Logger LOGGER = Logger.getLogger(SysWhiteController.class);

    @Resource
    SysUrlWhiteListService sysUrlWhiteListService;

    @RequestMapping(value = "/page")
    public ModelAndView page(ModelMap model) {
        String sysType = EnumToDataUtil.getDHEnumsValuesFromEnum(SysType.class);
        model.put("sysType", sysType);
        return new ModelAndView("whitelist/sysUrlMgr", model);
    }

    @PltLog(log = "分页查询URL白名单")
    @RequestMapping(value = "/findSysUrlGrid")
    public @ResponseBody Pager findSysUrlGrid(Pager pager, SysUrlWhiteListDto dto) {
        dto.setIsLikeQuery(1);
        return this.sysUrlWhiteListService.findPageDataHosUrlsDto(pager, dto);
    }

    @RequestMapping(value = "/openEditSysUrlWin")
    public ModelAndView openEditSysUrlWin(ModelMap model, String type) {
        model.put("type", type);
        return new ModelAndView("whitelist/win/editSysUrl", model);
    }

    @PltLog(log = "新增URL白名单")
    @RequestMapping(value = "/addSysUrl")
    public @ResponseBody ResultMessage addSysUrl(@RequestBody SysUrlWhiteListDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "保存成功");
        try {
            if (!checkSysUrl(dto, request, result, "addSysUrl")) {
                return result;
            }
            dto.setId(null);
            if (this.sysUrlWhiteListService.findPageDataCountByDto(dto) > 0) {
                result.change(false, new StringBuilder("当前系统白名单里该应用ID【").append(dto.getAppId()).append("】下已存在该URL,请修改！").toString());
                return result;
            }
            dto.setGmtCreate(new Date());
            dto.setGmtModify(new Date());
            String operator = getSessionUser(request).getUserName();
            dto.setOperator(operator);
            dto.setStatus(Status.ENABLED.getCode());//目前新增直接为有效状态
            this.sysUrlWhiteListService.insertSelective(dto);
        } catch (Exception e) {
            result.change(false, "保存失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysUrlWhiteListController-addSysUrl error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    @PltLog(log = "编辑URL白名单")
    @RequestMapping(value = "/editSysUrl")
    public @ResponseBody ResultMessage editSysUrl(@RequestBody SysUrlWhiteListDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "保存成功");
        try {
            if (!checkSysUrl(dto, request, result, "editSysUrl")) {
                return result;
            }
            if (this.sysUrlWhiteListService.findPageDataCountByDto(dto) > 0) {
                result.change(false, new StringBuilder("当前系统白名单里该应用ID【").append(dto.getAppId()).append("】下已存在该URL,请修改！").toString());
                return result;
            }
            dto.setGmtModify(new Date());
            String operator = getSessionUser(request).getUserName();
            dto.setOperator(operator);
            this.sysUrlWhiteListService.updateByPrimaryKeySelective(dto);
        } catch (Exception e) {
            result.change(false, "保存失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysUrlWhiteListController-editSysUrl error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    @PltLog(log = "删除URL白名单")
    @RequestMapping(value = "/delSysUrl")
    public @ResponseBody ResultMessage delSysUrl(@RequestBody SysUrlWhiteListDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "删除成功");
        try {
            CurrentKey key = new CurrentKey();
            key.setId(dto.getId());
            if (!checkParam(request, key)) {
                result.change(false, "用户无权访问！");
                return result;
            }
            dto.setStatus(Status.DELETE.getCode());
            dto.setGmtModify(new Date());
            String operator = getSessionUser(request).getUserName();
            dto.setOperator(operator);
            this.sysUrlWhiteListService.updateByPrimaryKeySelective(dto);
        } catch (Exception e) {
            result.change(false, "删除失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysUrlWhiteListController-delSysUrl error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    private boolean checkSysUrl(SysUrlWhiteListDto dto, HttpServletRequest request, ResultMessage result, String oper) {
        CurrentKey key = new CurrentKey();
        key.setId(dto.getId());
        if (!checkParam(request, key)) {
            result.change(false, "用户无权访问！");
            return false;
        }
        return true;
    }
}
